package com.darker.yygh.cmn.controller;


import com.atguigu.yygh.model.cmn.Dict;
import com.darker.yygh.cmn.service.DictService;
import com.darker.yygh.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//跨域
//@CrossOrigin
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    DictService dictService;
    //根据ID查询数据的子数据

    @ApiOperation("根据ID查询数据的子数据")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> dictList = dictService.findChildData(id);
        return Result.ok(dictList);
    }

    //导出数据字典结构
    @ApiOperation("导出数据字典结构")
    @GetMapping("/exportData")
    public void exportDict(HttpServletResponse response) {
        dictService.exportDictData(response);


    }
    //导入数据字典

    @ApiOperation("导入数据字典")
    @PostMapping("/importData")
    public Result importData( MultipartFile file) {

        dictService.importData(file);
        return Result.ok();
    }

    @ApiOperation("根据dictCode获取下级节点")
//    根据dictCode获取下级节点
    @GetMapping("findBydictCode/{dictCode}")
    public Result findBydictCode(@PathVariable String dictCode) {
        List<Dict> dictList = dictService.findBydictCode(dictCode);
        return Result.ok(dictList);
    }

    //根据dictcode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode, @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    //根据value查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getDictName("",value);
        return dictName;
    }

}
